//
//  MrMIDIViewCtrler.m
//  MIDI
//
//  Created by EMG on 15/9/8.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "MrMIDIViewCtrler.h"
#import "MrMIDMTopCell.h"
#import "MrMIDIBottomCell.h"
#import "AppConfig.h"
#import "APIAdress.h"
#import "QXNetWorkTool.h"
#import "MrMiDiDetailModel.h"
#import "MBProgressHUD+YC.h"
#import "UserDataSingleton.h"
#import "UIActionSheet+Blocks.h"
#import "UIImageView+WebCache.h"
#import "MrMIDIIntroduceCell.h"
#import "XWAlterview.h"

@interface MrMIDIViewCtrler()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate , QXNetWorkToolDelegate, UIAlertViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *thisTableView;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@property (strong , nonatomic) MrMiDiDetailModel *mdata;
@property (copy , nonatomic) NSString *mr_id;

@property (strong , nonatomic) MrMIDIBottomCell * bottomCell;
@property (strong , nonatomic) MrMIDIIntroduceCell * introduceCell;


@end

@implementation MrMIDIViewCtrler


-(void)viewDidLoad
{
    
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
    self.thisTableView.delegate = self;
    self.thisTableView.dataSource = self;
    self.thisTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
    if(self.mdata == nil && self.mr_id != nil)
        [self reqData];
}


-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"迷笛先生详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    
    //电话图标
    UIButton *phoneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    
    [phoneButton setImage:[UIImage imageNamed:@"phone_icon"] forState:UIControlStateNormal];
    
    phoneButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
    
    [phoneButton addTarget:self action:@selector(pressRight:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarPhone = [[UIBarButtonItem alloc]initWithCustomView:phoneButton];
    rightBarPhone.tintColor = UIColorRGBA(118, 207, 201, 1);

    
    //微信图标
    UIButton *weixinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    
    [weixinButton setImage:[UIImage imageNamed:@"ico_mr_midi_detail_weixin"] forState:UIControlStateNormal];
    
    weixinButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
    
    [weixinButton addTarget:self action:@selector(clickWeiXin:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarWeiXin = [[UIBarButtonItem alloc]initWithCustomView:weixinButton];
    rightBarWeiXin.tintColor = UIColorRGBA(118, 207, 201, 1);
    
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:rightBarPhone, rightBarWeiXin, nil]];
}

- (void)clickWeiXin:(UIButton *)sender
{
    if (self.mdata && self.mdata.weixin_num != nil)
    {        
        NSString *str = [NSString stringWithFormat:@"%@:%@", @"微信", self.mdata.weixin_num];
        
        XWAlterview *alert = [[XWAlterview alloc] initWithTitle:@"" contentText:str leftButtonTitle:nil rightButtonTitle:@"长按复制"];
        
        [alert show];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [textField resignFirstResponder]; //这句代码可以隐藏 键盘
    return NO;
}

-(void)pressRight:(UIButton *)sender
{
    [self showSheet:nil];
}

- (void)showSheet:(id)sender
{
    if(self.mdata == nil)
        return;
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:self.mdata.phone,nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    
    [actionSheet showInView:self.view];
    
    actionSheet.tapBlock = ^(UIActionSheet *actionSheet, NSInteger buttonIndex)
    {
        switch (buttonIndex) {
            case 0:
                [self telePhoneCall];
                break;
                
            default:
                break;
        }
    };
}

//打电话
-(void)telePhoneCall
{
    //打电话，有提示框，电话结束返回APP
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.mdata.phone];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

-(void)reqData
{
    if(self.mr_id == nil)
        return;
    
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:MrMidiDetailInterface andParameter:@[@{@"mr_id":self.mr_id}] tag:1];
}


#pragma mark OCDeleaget

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        static NSString * identifier = @"MrMIDMTopCell";
        MrMIDMTopCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[[NSBundle mainBundle]loadNibNamed:identifier owner:nil options:nil] firstObject];
        }
        [self configCell:indexPath.row cell:cell];
        return cell;
    }
    else if (indexPath.row == 1)
    {
        static NSString * identifier = @"MrMIDIBottomCell";
        self.bottomCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (self.bottomCell == nil)
        {
            self.bottomCell = [[[NSBundle mainBundle]loadNibNamed:identifier owner:nil options:nil] firstObject];
        }
        [self configCell:indexPath.row cell:self.bottomCell];
        
        return self.bottomCell;
    }
    else
    {
        static NSString * identifier = @"MrMIDIIntroduceCell";
        MrMIDIIntroduceCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[[NSBundle mainBundle]loadNibNamed:identifier owner:nil options:nil] firstObject];
        }
        [self configCell:indexPath.row cell:cell];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 330;
    }
    else if (indexPath.row == 1)
    {
//        if (self.bottomCell)
//        {
//            return [self.bottomCell getHeight];
//        }
        return 213;
    }
    else
    {
        if (self.introduceCell)
        {
            return [self.introduceCell getHeight];
        }
        return 37;
    }
}

-(void) configCell:(NSInteger)row cell:(id)myCell
{
    if(self.mdata == nil)
        return;
    if(row == 0)
    {
        MrMIDMTopCell * mcell = myCell;
        [mcell.midiIDLb setText:[@"Midi ID: " stringByAppendingString: self.mdata.midi_id]];
        
        if (self.mdata.head_img != nil && ![self.mdata.head_img isEqualToString:@""])
        {
            [mcell.headIcon sd_setImageWithURL:[NSURL URLWithString:self.mdata.head_img]];
        }

        [mcell.nameLb setText:self.mdata.real_name];
        [mcell.jobLb setText:self.mdata.major_name];
        [mcell.addrLb setText:self.mdata.name];
        if([self.mdata.sex isEqual:@"1"])
        {
            [mcell.sexLb setText:@"男"];
        }
        else if([self.mdata.sex isEqual:@"2"])
        {
            [mcell.sexLb setText:@"女"];
        }
    }
    else if(row == 1)
    {
        MrMIDIBottomCell * mcell = myCell;
        
        [mcell.maxLevelLb setText:self.mdata.highest_level];
        [mcell.cycleLb setText:self.mdata.agree_cycle];
        [mcell.idcodeLb setText:self.mdata.certificate_num];
        [mcell.belong_institutionLb setText:self.mdata.belong_institution];
    }
    else
    {
        MrMIDIIntroduceCell * mcell = myCell;
        [mcell.introduce setText:self.mdata.mr_introduce];
        
        self.introduceCell = myCell;
        
        [self.introduceCell setFirstLineHeadindent];
    }
}

#pragma mark ----QxNetWorkTool ----------------------------
-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (tag == 1)
    {
        for(NSDictionary * dic in Response)
        {
            self.mdata = [MrMiDiDetailModel MyModelWithDict:dic];
            break;
        }
        [self.thisTableView reloadData];
    }
}

-(void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    [MBProgressHUD showError:(NSString*)Response];
}




//设置迷笛id
-(void)setMrMidiId:(NSString *)mrmidiId
{
    if(mrmidiId == nil)
        return;
    self.mr_id = mrmidiId;
}

@end















